
var bsurl = require('../../../utils/baseUrl.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cateIndex: 0,
    cateArray: ['手机数码', '电脑办公','日用品','食品','家电', '男装', '女装', '男鞋', '女鞋'],
    goods: {
      goodsname: "",
      goodssort: "手机数码",
      price: "",
      goodsdescribe: "",
      num: "",
      image: ""
    },
    tips: "提示",
    hidden: false,
    uploadImg:"/img/uploadImg.png",
    imgStyle:"upload-img"
  },
  //输入监听
  textinput: function (event) {
    var type = event.currentTarget.dataset.type;
    var good1 = this.data.goods;
    switch (Number(type)) {
      case 1: good1.goodsname = event.detail.value; break;
      case 2: good1.price = event.detail.value; break;
      case 3: good1.num = event.detail.value; break;
      case 4: good1.goodsdescribe = event.detail.value;break;
    }
    this.setData({ goods: good1 });
  },
  //选择类别
  bindCatePickerChange: function (e) {
    this.data.goods.goodssort = this.data.cateArray[e.detail.value]
    console.log(e)
    this.setData({
      cateIndex: e.detail.value,
      goods: this.data.goods
    })
  },
  add: function () {
    this.setData({
      hidden: false
    })
    var str = "";
    if (this.data.goods.goodsname == "") {
      str = '标题不能为空';
    }
    if (this.data.goods.goodsdescribe == "") {
      str = '描述不能为空';
    }
    if (!/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(this.data.goods.price)) {
      str = '价格格式不对';
    }
    if (!/^[1-9]*[1-9][0-9]*$/.test(this.data.goods.num)) {
      str = '数量格式不对';
    }
    if (this.data.uploadImg == "/img/uploadImg.png"){
      str = '请添加图片';
    }
    if (str != "") {
      this.setData({
        hidden: true,
        tips: str
      })
      return;
    }
    wx.showToast({
      title: '上传中...',
      icon: "loading",
      duration:60000
    });
    console.log(app.globalData.userInfo);
    var _this = this;
    wx.uploadFile({
      url: bsurl +"file/uploadGoods.do",
      filePath: this.data.uploadImg,
      header:{
        JSESSIONID: app.globalData.JSESSIONID
      },
      name: 'img',
      formData: {
        username: app.globalData.userInfo.shopname,
        goodsname: this.data.goods.goodsname,
        goodssort: this.data.goods.goodssort,
        price: this.data.goods.price,
        goodsdescribe: this.data.goods.goodsdescribe,
        num: this.data.goods.num
      },
      success: function (res) {
        console.log(res);
        wx.hideToast();
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: "请求失败",
          });
          return;
        }
        console.log(res.data)
        
        var data = JSON.parse(res.data);
        if (data.success) {
          wx.showToast({
            title: data.message,
            duration: 2000,
            success: function (res) {
              _this.reset();
            }
          })
        }
        else {
          wx.showModal({
            title: '提示',
            content: data.message,
          });
        }
      },
      fail: function (res) {
        wx.hideToast();
        wx.showModal({
          title: '提示',
          content: data.message,
        });
      }
    })

  },
  reset:function(){
    this.setData({
      
      uploadImg: "/img/uploadImg.png",
      imgStyle: "upload-img"
    }) 
  },
  //选择图片
  uploadImg: function () {
    var _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage('camera')
          }
        }
      }
    })
  },
  //选择图片
  chooseWxImage: function (type) {
    let _this = this;
    wx.chooseImage({
      count:1,
      sizeType: ['compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res);
        _this.setData({
          imgStyle:"selected-img",
          uploadImg: res.tempFilePaths[0],
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  }
})